<?php

namespace api\models;

use Yii;
use yii\base\Model;
use common\models\User;


class LoginForm extends Model{
    
    public $user;
    public $email;
    public $password;
    
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            ['email', 'required'],
            ['password', 'required'],
            ['email','checkUser']
        ];
    }
    
    /**
     * 
     */
    public function checkUser($attribute){
        $this->user = User::find()->where(['email' => $this->email])->one();
        if($this->user && $this->user->comparePassword($this->password)){
            return true;
        }
        $this->addError($attribute,'Tài khoản của bạn không đúng');
        return false;
    }
}